package org.wmine.preprocess;

import org.wmine.core.BasicTabPanel;
import org.wmine.eventhandlers.MissingValueEventHandler;

import org.wmine.core.IProjectManager;
import org.wmine.core.ProjectManager;
import org.wmine.core.MainWindow;
import org.wmine.core.WMineConstants;
import org.wmine.core.CSVReader;
import org.wmine.data.Attribute;
import org.wmine.data.DataSet;
import org.wmine.data.InvalidCursorPositionException;
import org.wmine.data.Table;
import org.wmine.data.TableResultSet;
import org.wmine.data.XMLDataReader;
import org.xml.sax.SAXException;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
//import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.*;
import javax.xml.parsers.ParserConfigurationException;

import java.awt.*;
import java.io.IOException;
import java.io.StringBufferInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import org.wmine.data.Attribute;
import javax.swing.table.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import org.wmine.core.IntegerTextField;
public class PreprocessPanel extends BasicTabPanel  {

	JTable tableData;
	JScrollPane scrollPane=null,scroll=null;	
	DefaultTableModel dtm;
	String dataValues[][]=null;
	String[] columnNames=null;
	Object [] items={"Missing Values","Binning","Discretization","Generalization","Dimensionality Reduction","Numerosity Reduction","Normalization"};
	int cntRow,cntCol;
	String tablename;
	public Iterator<Attribute> itrAttr;
	public static Iterator<Attribute> itrNew;
	JLabel descri1,lblSample;
	JRadioButton radioRemove,radioEdit,radioGlobal,radioMean,radioMedian;
	ButtonGroup bg;
	JButton btnMissing,btnOK;
	JPanel mainPanel,pan1,pan2,pan3,pan4;
	JComboBox com2;
	IntegerTextField txtSample;
	JTabbedPane jtbWorkspace;
	public static JComboBox com1;
	
	public static JLabel report_summary;
	public static Summary summary;
	public static Workspace workspace;
	public static int flag=1;
	public static StringBuffer report;
	String fname;
	float []f=new float[3];
	float []fpan=new float[3];
	float []fmain=new float[3];
	String ColumnName=null;
	
	public String getTitle() {
		return "Preprecessing";
	}

	public String getTooltip() {
		return "Import and clean the data";
	}

	public JPanel buildWindow() {
		
		
	//	f=Color.RGBtoHSB(215,215,255,f);
	//	fmain=Color.RGBtoHSB(85,0,85,fmain);
	//	fpan=Color.RGBtoHSB(232,232,255,fpan);
		//newly added
		
		mainPanel=new JPanel();
		mainPanel.setLayout(null);
	//	mainPanel.setBackground(Color.getHSBColor(fmain[0],fmain[1],fmain[2]));
		pan1=new JPanel();
		pan1.setLayout(null);
		pan1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Input"));
		pan1.setBounds(10,10,230,315);
		//pan1.setBackground(Color.getHSBColor(fpan[0],fpan[1],fpan[2]));
		
		pan2=new JPanel();
		pan2.setLayout(null);
		pan2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Description"));
		descri1=new JLabel();
	//	pan2.setBackground(Color.getHSBColor(fpan[0],fpan[1],fpan[2]));
		
		descri1.setBounds(30,30,200,170);
		pan2.setBounds(10,330,230,230);
		pan2.add(descri1);
		
		pan3=new JPanel();
		pan3.setLayout(null);
		pan3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Workspace"));
		pan3.setBounds(245,10,540,315);
		//pan3.setBounds(245,10,550,550);
		//pan3.setBackground(Color.getHSBColor(fpan[0],fpan[1],fpan[2]));
		
		pan4=new JPanel();
		pan4.setLayout(null);
		pan4.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Operations"));
		pan4.setBounds(245,330,540,230);
		//pan4.setBackground(Color.getHSBColor(fpan[0],fpan[1],fpan[2]));
		
		JLabel lab1=new JLabel("Select the file:");
		
		lab1.setBounds(10,50,160,30);
		com1=new JComboBox();
		String []fnames=null;
		String []fnamesraw=null;
	//	System.out.println("8888*****"+ProjectManager.getUniqueInstance().isProjectOpened());
		if(ProjectManager.getUniqueInstance().isProjectOpened())
		{	
			fnames=ProjectManager.getUniqueInstance().getFileNamesArray("preprocess");
			fnamesraw=ProjectManager.getUniqueInstance().getFileNamesArray("raw");
			for(int i=0;i<fnames.length;i++)
			{
				com1.addItem(fnames[i]);
				//System.out.println("====>"+fnames[i]);
			}
			for(int i=0;i<fnamesraw.length;i++)
			{
				com1.addItem(fnamesraw[i]);
				System.out.println("====>"+fnamesraw[i]);
			}
		}
		
		com1.setEditable(false);
		com1.setBounds(10,90,160,20);
		com1.addActionListener(new ActionListener()
		//com1.addItemListener(new ItemListener()
        {
			//public void itemStateChanged(ItemEvent e1)
			public void actionPerformed(ActionEvent e1) 
			{
				
				com2.setEnabled(true);
				
				JComboBox cmb=(JComboBox)e1.getSource();
				
		    	 MainWindow.getInstance().showOnStatusBar(cmb.getSelectedItem().toString());
		    	 fname=com1.getSelectedItem().toString();
		    	 
		    	 descri1.setText("<html><body>You have selected file :<b>"+fname+"</b></body></html>");
		    	 try
		 		{
		    		dataValues=null;
				 	columnNames=null;
				 	
				 	LoadData(fname);
				 	//pan3.repaint();
		 		}catch(Exception ee){}
		 		
		}}); // end of com1 listner
		
		
		JLabel lab2=new JLabel("Select the data cleaning technique:");
		lab2.setBounds(10,150,230,30);
		
		com2=new JComboBox();
		
		for(int i=0;i<items.length;i++)
			com2.addItem(items[i]);
		com2.setEditable(false);
		com2.setBounds(10,190,160,20);
		com2.setEnabled(false);
		pan1.add(lab1);
		pan1.add(com1);
		

		//System.out.print("in missing ");
		com2.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ee)
			{
				String AlgoLabel=com2.getSelectedItem().toString();
				//tableData.removeAll();
				pan2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),AlgoLabel));	
				//if()  
				
					     
					    // handling file combo 
				int i=com2.getSelectedIndex();     
				if(i==0) // display screen for missing radio buton
				{
					pan4.removeAll();
					pan4.repaint();
					report_summary.setText("");
					summary.repaint();
					
					descri1.setText("<html><body><b>"+AlgoLabel+": </b> Removes or allows user to edit missing values</body></html>");
					
					/* showing summary of how many missing records found 
					*/
					Missing_Functions f=new Missing_Functions(dtm);
					int cntMissing=f.CountMissingValues();
					
					report.append("Number of Missing Values: "+cntMissing+"<br>");
					report_summary.setText(report.toString());
					
					bg=new ButtonGroup();
				    radioRemove=new JRadioButton("Remove Missing Values");
				    radioEdit=new JRadioButton("Edit Missing Values");
				    radioGlobal=new JRadioButton("Replace by Global value");
				    radioMean=new JRadioButton("Replace by Mean value");
				    
				       		
				    radioRemove.addActionListener(new ActionListener()
				    {
				       public void actionPerformed(ActionEvent ae)
				       {
				    	   btnMissing.setLabel("Remove Missing");
				    	   tableData.setEnabled(false);
				       }
				    }); // end of remove radio missing listener
				    
				    radioEdit.addActionListener(new ActionListener()
				    {
				       			public void actionPerformed(ActionEvent ae)
				       			{
				       				btnMissing.setLabel("Save Values");
				       				tableData.setEnabled(true);
				       			}
				     }); //end of edit missing radio listener
				     
				    radioGlobal.addActionListener(new ActionListener()
				    {
				    	public void actionPerformed(ActionEvent ae)
						{
							btnMissing.setLabel("Save Values");
							tableData.setEnabled(true);
							Missing miss=new Missing(dtm);
							//MissingValuesA miss=new MissingValuesA();
							miss.setVisible(true);
							
						}
					}); //end of global missing radio listener
				   				    
				    radioMean.addActionListener(new ActionListener()
				    {
					  	public void actionPerformed(ActionEvent ae)
					  	{
							btnMissing.setLabel("Save Values");
							//tableData.setEnabled(true);
							Mean_Median1 miss=new Mean_Median1("Replace by Mean",dtm);
							miss.setVisible(true);
						}
					});//end of global missing radio listener
				    
				     radioRemove.setBounds(50,50,200,25);
				     radioEdit.setBounds(300,50,200,25);
				     radioMean.setBounds(50,85,200,25);
				     radioGlobal.setBounds(300,85,200,25);
				     radioRemove.setSelected(true);
				       		
				     bg.add(radioRemove);
				     bg.add(radioEdit);
				     bg.add(radioGlobal);
				     bg.add(radioMean);
				     
				     
				     btnMissing =new JButton("Remove Missing");
				     btnMissing.setBounds(180,130,150,25);
				     btnMissing.addActionListener(new ActionListener()
				     {
				    	 public void actionPerformed(ActionEvent aee)
				    	 {
				    		String str=aee.getActionCommand();
				    			 	
				    		ArrayList lst=new ArrayList();
				    				
				    		int flag=0;
				    		if(str.equals("Remove Missing"))
				    		{
				    		//	System.out.println("in rem miss "+dtm.getDataVector()+ "\ncntrow:"+cntRow);
				    			try
				    			{
				    				if(dtm.getRowCount()>0)
				    				{
				    					for(int i=0;i<cntRow;i++)
				    					{
				    						for(int j=0;j<cntCol;j++)
				    						{
				    						//	System.out.println("for j:"+i+" "+j+ " "+dtm.getValueAt(i,j));
				    							if(dtm.getValueAt(i,j).toString().equals("?") || dtm.getValueAt(i,j).toString().trim().equals("") || dtm.getValueAt(i,j).toString().trim().equals(null))
				    							{	
				    								lst.add(i);
				    								//tableData.setSelectionBackground(Color.red);
				    								tableData.setGridColor(Color.RED);
				    										//dtm.
				    										
				    						//		System.out.println("in if: "+i);
				    								
				    								flag=1;
				    							}
				    						}		
				    					}
				    				}
				    				else
				    					JOptionPane.showMessageDialog(null,"No data found");	
				    				
				    				/* display entire row that is deleted in report_summary*/
				    				
				    				
				    				RemoveMissingRows(lst);	
				    				btnMissing.setLabel("Save Values");
				    					
				    			}
				    			catch(Exception e)
				    			{
				    				System.out.println(e);
				    			}
				    		} // end of handling remove missing
				    		
				    		else if(str.equals("Save Values"))
				    		{
				    			Missing_Functions f=new Missing_Functions(dtm);
				    			if(dtm.getRowCount()<=0)
				    			{
				    				JOptionPane.showMessageDialog(null,"No data Found!!");	
				    			}
				    			else if(f.ContainsMissingValues())
				    			{
				    				JOptionPane.showMessageDialog(null,"There are still missing values...Please fill them...");	
				    			}
				    			else
				    			{
				    				// write code here to convert data from table to xml
				    				String filename=JOptionPane.showInputDialog("Enter filename");
				    					
				    				if(filename!=null)
				    				{
				    					org.wmine.core.XmlConvert x=new org.wmine.core.XmlConvert(tablename);
				    					//x.CreateTableXML(filename,cntCol,cntRow,itrAttr,dtm);
				    					x.CreateTableXML1(filename,cntCol,cntRow,itrAttr,dtm);
				    					JOptionPane.showMessageDialog(null,"Data Converted to "+filename+".xml");
				    					//com1.addItem(filename+".xml");
				    				//	MainWindow.getInstance().setupTabPanels();
				    				//	MainWindow.getInstance().mainTabbedPane.remove(0);
				    				///	MainWindow.getInstance().mainTabbedPane.remove(0);
				    				//	MainWindow.getInstance().mainTabbedPane.remove(0);
				    				}
				    			}
				    		} //end of handling saving in xml file
				    	}
				     });	// end of missing button listner
				    		
					pan4.add(btnMissing);
					pan4.add(radioRemove);
					pan4.add(radioEdit);
					pan4.add(radioGlobal);
					
					pan4.add(radioMean);
					pan4.repaint();
					
				}
				else if(i==2) //discretization
				{
					
					//System.out.println(""+);
					report_summary.setText("");
					summary.repaint();
					
					pan4.removeAll();
					pan4.repaint();
					descri1.setText("<html><body><b>"+AlgoLabel+": </b> Performs discretization of data </body></html>");
				   					   	
				   	btnMissing =new JButton("Save in File");
					btnMissing.setBounds(180,70,150,25);
					pan4.add(btnMissing);
					pan4.repaint();
					
					Discretization d=new Discretization(columnNames,dtm);
					ColumnName=d.getSelectedColumn();
					d.setVisible(true);
				   	
					btnMissing.addActionListener(new ActionListener()
					{

						public void actionPerformed(ActionEvent arg0) {
									// TODO Auto-generated method stub
						String filename=JOptionPane.showInputDialog("Enter filename");
			    					
				    	if(filename!=null)
				    	{
				    			org.wmine.core.XmlConvert x=new org.wmine.core.XmlConvert(tablename);
				    					//x.CreateTableXML(filename,cntCol,cntRow,itrAttr,dtm);
				    			
				    			x.CreateTableXML2(filename,itrAttr,dtm,"Discretization","Real",ColumnName);
				    			JOptionPane.showMessageDialog(null,"Data Converted to "+filename+".xml");
				    			com1.addItem(filename+".xml");
				    	}
						}
					});    
					
				}
				else if(i==1) //binning
				{
					pan4.removeAll();
					pan4.repaint();
					report_summary.setText("");
					summary.repaint();
					descri1.setText("<html><body><b>"+AlgoLabel+": </b> Performs Binning <br>to smoothen the data and <br>remove noise </body></html>");
				   	
					btnMissing =new JButton("Save in File");
				    btnMissing.setBounds(180,70,150,25);
				    
					pan4.add(btnMissing);
					pan4.repaint();
					
				
					Binning bin= new Binning(columnNames,dtm,tablename);
					bin.setVisible(true);
									
					mainPanel.setVisible(true);
					
					btnMissing.addActionListener(new ActionListener()
					{
							public void actionPerformed(ActionEvent aee)
						    {
								if(flag==1)
								{
									String filename=JOptionPane.showInputDialog("Enter filename");
			    					
				    				if(filename!=null)
				    				{
				    					
				    					org.wmine.core.XmlConvert x=new org.wmine.core.XmlConvert(tablename);
				    					x.CreateTableXML1(filename,dtm.getColumnCount(),dtm.getRowCount(),itrAttr,dtm);
				    					JOptionPane.showMessageDialog(null,"Data Converted to "+filename+".xml");
				    					com1.addItem(filename+".xml");
				    				}
								}
								else
									JOptionPane.showMessageDialog(null,"Error!! In performing Binning");
						    }
					});
				}
				else if(i==3) // generalization
				{
					pan4.removeAll();
					pan4.repaint();
					descri1.setText("<html><body><b>"+AlgoLabel+": </b> Performs Generalization on data </body></html>");
					report_summary.setText("");
					summary.repaint();
					
					
				   	btnMissing =new JButton("Save in File");
					btnMissing.setBounds(180,70,150,25);
					
					pan4.add(btnMissing);
					pan4.repaint();
					
					Generalization g=new Generalization(columnNames,dtm);
					ColumnName=g.getSelectedColumn();
					g.setVisible(true);
					
					btnMissing.addActionListener(new ActionListener()
					{

						public void actionPerformed(ActionEvent arg0) {
							// TODO Auto-generated method stub
							String filename=JOptionPane.showInputDialog("Enter filename");
	    					
		    				if(filename!=null)
		    				{
		    					org.wmine.core.XmlConvert x=new org.wmine.core.XmlConvert(tablename);
		    					//x.CreateTableXML(filename,cntCol,cntRow,itrAttr,dtm);
		    					x.CreateTableXML2(filename,itrAttr,dtm,"Generalization","String",ColumnName);
		    					JOptionPane.showMessageDialog(null,"Data Converted to "+filename+".xml");
		    					com1.addItem(filename+".xml");
		    				}
						}
						
					});
					
				}
				else if(i==4) // Dimensionality reduction
				{
					pan4.removeAll();
					pan4.repaint();
					descri1.setText("<html><body><b>"+AlgoLabel+": </b> Removes not needed columns</body></html>");

					report_summary.setText("");
					summary.repaint();
					
					Dimensionality d=new Dimensionality(dtm);
					d.setVisible(true);
					btnMissing =new JButton("Save in File");
					btnMissing.setBounds(180,70,150,25);
					btnMissing.addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent aee)
						{
							String filename=JOptionPane.showInputDialog("Enter filename");
					    			
							if(filename!=null)
							{
								org.wmine.core.XmlConvert x=new org.wmine.core.XmlConvert(tablename);
						    	x.CreateTableXML1(filename,dtm.getColumnCount(),dtm.getRowCount(),itrAttr,dtm);
						    	JOptionPane.showMessageDialog(null,"Data Converted to "+filename+".xml");
						    	com1.addItem(filename+".xml");
						    }
						}
					}); 
					pan4.add(btnMissing);
					pan4.repaint();
				}
				else if(i==5) // numerosity reduction
				{
					pan4.removeAll();
					pan4.repaint();
					descri1.setText("<html><body><b>"+AlgoLabel+": </b> Performs Numerosity Reduction on data </body></html>");
				   	
					report_summary.setText("");
					summary.repaint();
					
					bg=new ButtonGroup();
					
					radioRemove=new JRadioButton("Manual Selection");
					radioEdit=new JRadioButton("Random Selection");
										
					radioRemove.setBounds(50,20,200,25);
				    radioEdit.setBounds(260,20,200,25);
				    radioRemove.setSelected(true);
				    
				    tableData.setEnabled(false);
				    
				    lblSample=new JLabel("How many samples you want?");
				   	lblSample.setBounds(50,55,200,25);
				   	lblSample.setEnabled(false);  
				   	
				   	txtSample=new IntegerTextField();
				   	txtSample.setBounds(260,55,100,25);
				   	txtSample.setEnabled(false);
				   	
				    //manual selection enable table for selection
				    radioRemove.addActionListener(new ActionListener()
				    {
					       public void actionPerformed(ActionEvent ae)
					       {
						   	   tableData.setEnabled(true);
						   	   txtSample.setEnabled(false);
						   	   lblSample.setEnabled(false);
						   	   btnMissing.setVisible(false);
						   	   JOptionPane.showMessageDialog(null,"Select Rows you want to REMOVE");
						   }
				    }); // end of manual selection radio listener
				    
				    radioEdit.addActionListener(new ActionListener()
					{
						  public void actionPerformed(ActionEvent ae)
						  {
							  tableData.setEnabled(false);
							  txtSample.setEnabled(true);
							  lblSample.setEnabled(true);
							  btnMissing.setVisible(false);
						  }
					}); // end of random selection radio  listener
				    
				    
				    bg.add(radioRemove);
				    bg.add(radioEdit);
				    
				    btnOK=new JButton("OK");
				    btnOK.setBounds(180,100,150,25);
				    
				    
				   	  
				   	btnMissing=new JButton("Save in File");
				    btnMissing.setBounds(180,140,150,25);
				    btnMissing.setVisible(false);
				    btnMissing.addActionListener(new ActionListener()
					{

						public void actionPerformed(ActionEvent arg0) {
									// TODO Auto-generated method stub
						String filename=JOptionPane.showInputDialog("Enter filename");
			    					
				    	if(filename!=null)
				    	{
				    		org.wmine.core.XmlConvert x=new org.wmine.core.XmlConvert(tablename);
				    					//x.CreateTableXML(filename,cntCol,cntRow,itrAttr,dtm);
				    		x.CreateTableXML1(filename,cntCol,cntRow,itrAttr,dtm);
				    		JOptionPane.showMessageDialog(null,"Data Converted to "+filename+".xml");
				    		com1.addItem(filename+".xml");
				    	}
						}
					});
				    
				    btnOK.addActionListener(new ActionListener()
				    {
				    	public void actionPerformed(ActionEvent ae)
						{
						  	   btnMissing.setVisible(true);
						  	   //manual selection
						  	   if(radioRemove.isSelected()==true)
						  	   {
						  		  int selected_rows[]=tableData.getSelectedRows();
						  		  Numerosity n= new Numerosity(dtm);
						  		  n.Numerosity_Manual(selected_rows);
						  	   }
						  	   //random selection
						  	   else if(radioEdit.isSelected()==true)
						  	   {
						  		   if(!(txtSample.getText().trim().equals(null)) || !(txtSample.getText().trim().equals("")) )
						  		   {
						  			   int samples=Integer.parseInt(txtSample.getText().trim());
						  			   if(samples<=dtm.getRowCount())
						  			   {
						  				   Numerosity n= new Numerosity(dtm);
						  				   n.Numerosity_Random(samples);
						  			   }
						  			   else
						  			   {
						  				 JOptionPane.showMessageDialog(null,"No.of Samples cannot be greater than No.of Records!!");
						  			   }
						  		   }
						  		   else
						  		   {
						  			 JOptionPane.showMessageDialog(null,"No.of Samples cannot be blank!!");
						  		   }
						  		   
						  	   }
						  	   
						}
				   }); // end of ok button Listener
				    
				    pan4.add(btnMissing);
				    pan4.add(btnOK);
					pan4.add(radioRemove);
					pan4.add(radioEdit);
					pan4.add(lblSample);
					pan4.add(txtSample);
					    
					pan4.repaint();
					
					
				}
				
				else if(i==6) // normalization
				{
					pan4.removeAll();
					pan4.repaint();
					descri1.setText("<html><body><b>"+AlgoLabel+": </b> Data is scaled to fall within small specific range</body></html>");

					report_summary.setText("");
					summary.repaint();
					
					radioRemove=new JRadioButton("Min-Max Normalization");
					radioEdit=new JRadioButton("Z-Score Normalization");
					radioMean=new JRadioButton("Decimal Scaling");
					
					bg=new ButtonGroup();
					radioRemove.setBounds(50,50,200,25);
				    radioEdit.setBounds(260,50,200,25);
				    radioMean.setBounds(50,90,200,25);
				    
				    bg.add(radioRemove);
				    bg.add(radioEdit);
				    bg.add(radioMean);
				    
				    //radioRemove.setSelected(true);
				    radioRemove.addActionListener(new ActionListener()
				    {

						public void actionPerformed(ActionEvent arg0) {
							// TODO Auto-generated method stub
							String []col=new String[dtm.getColumnCount()];
							for(int i=0;i<dtm.getColumnCount();i++)
								col[i]=dtm.getColumnName(i);
							MinMax m=new MinMax(col,dtm);
							ColumnName=m.getSelectedColumn();
							m.setVisible(true);
						}
				    	
				    });
				    
				    radioMean.addActionListener(new ActionListener()
					{

						public void actionPerformed(ActionEvent arg0) {
									// TODO Auto-generated method stub
						String []col=new String[dtm.getColumnCount()];
						for(int i=0;i<dtm.getColumnCount();i++)
							col[i]=dtm.getColumnName(i);
						DecimalScaling m=new DecimalScaling(col,dtm);
						ColumnName=m.getSelectedColumn();
						m.setVisible(true);
						}
					});
				    
				    radioEdit.addActionListener(new ActionListener()
					{

						public void actionPerformed(ActionEvent arg0) {
										// TODO Auto-generated method stub
							String []col=new String[dtm.getColumnCount()];
							for(int i=0;i<dtm.getColumnCount();i++)
								col[i]=dtm.getColumnName(i);
							ZScore m=new ZScore(col,dtm);
							ColumnName=m.getSelectedColumn();
							m.setVisible(true);
						}
					});
				    btnMissing=new JButton("Save in File");
				    btnMissing.setBounds(180,140,100,25);
				    btnMissing.addActionListener(new ActionListener()
					{
				    	public void actionPerformed(ActionEvent arg0) {
									// TODO Auto-generated method stub
						String filename=JOptionPane.showInputDialog("Enter filename");
			    					
				    	if(filename!=null)
				    	{
				    			org.wmine.core.XmlConvert x=new org.wmine.core.XmlConvert(tablename);
				    					//x.CreateTableXML(filename,cntCol,cntRow,itrAttr,dtm);
				    			x.CreateTableXML2(filename,itrAttr,dtm,"Normalization","Real",ColumnName);
				    			JOptionPane.showMessageDialog(null,"Data Converted to "+filename+".xml");
				    			com1.addItem(filename+".xml");
				    	}
						}
					});
				    tableData.setEnabled(false);
				    pan4.add(radioMean);
				    pan4.add(radioRemove);
				    pan4.add(radioEdit);
				    pan4.add(btnMissing);
				    pan4.repaint();
				}
				
				
				
		}}); // end of handling com2 listner
		
		pan1.add(lab2);
		pan1.add(com2);
		
		
		mainPanel.add(pan1);
		mainPanel.add(pan2);
		mainPanel.add(pan3);
		mainPanel.add(pan4);
		
		mainPanel.setBounds(0,0,500,700);
		
		return mainPanel;
	}	
		
			
	
	 void LoadData(String fname) throws ParserConfigurationException, SAXException, IOException
	{
		 tableData=null;
		 columnNames=null;
		 dtm=null;
		 tableData=null;
		 scrollPane=null;
		 pan3.removeAll();
		 pan3.repaint();
		DataSet dataSet = new DataSet();
		
		String filename=fname; // have to append initial path not done yet
		if(filename.endsWith(".xml"))
		{
			//System.out.print("in load data");
			/*if(filename.equals("File1.xml"))
				filename="xmldoc02.xml";
				//filename="D:\\workspace\\xmldoc02.xml";
			else if((filename.equals("File3.xml")))
				filename="xmldoc01.xml";
				//filename="D:\\workspace\\xmldoc01.xml";
			else*/
			
			filename=fname;
			IProjectManager ip=ProjectManager.getUniqueInstance();
			
			dataSet.setDataReader(new XMLDataReader(new StringBufferInputStream(ip.readFile(filename))));
			System.out.print("in load data");
		}
		else if(filename.endsWith(".csv") || filename.endsWith(".dat"))
		{
			/*filename="name1.dat";
			tablename="name1.dat";*/
			System.out.print("in else");
			dataSet.setDataReader(new CSVReader(filename));
			
		}
		
		// "E:\\semester 3\\rajeev\\rajeev\\WMine\\xmldoc02.xml"
		
		
		
		System.out.println("data loaded");
		cntCol=dataSet.getAttributeCount();
		cntRow=dataSet.getTupleCount();
		System.out.println(cntCol+ " " +cntRow);
		
		columnNames=new String[cntCol];
		Iterator<Attribute> colnames= dataSet.getAttributeIterator();
		itrAttr=dataSet.getAttributeIterator();
		itrNew=dataSet.getAttributeIterator();
		int i=0;
		
		while(colnames.hasNext())
		{
			Attribute s=colnames.next();
			//System.out.println("s:"+s.getAttributeName());
			
			columnNames[i++]=s.getAttributeName();
		}
		
		//Table table=new Table();
		//String tablename=table.getNameTable();
		tablename=dataSet.getTableName();
		//System.out.println("tablename: "+tablename);
		TableResultSet resultSet = dataSet.getTableResultSet();
		//resultSet.reset();
		// creating memory for dataValues
		dataValues=new String[cntRow][cntCol];
	 	int j=0;
	 	
		while(resultSet.hasNext()) 
		{
			try 
			{
			//	System.out.println("in while try");
				for(i=0;i<cntCol;i++)
				{
					if(resultSet.getString(i).equals("null"))
						dataValues[j][i]="?";
					else
						dataValues[j][i]=resultSet.getString(i);
			//		System.out.println("Tuple :"+ resultSet.getString(i));
				}
			}catch (InvalidCursorPositionException e) 
			{
				e.printStackTrace();
			}
			j++;
		}
		
	//	System.out.println("datavalues:"+dataValues[0][0]);
		dtm=new DefaultTableModel(dataValues,columnNames);
 		tableData = new JTable(dtm);
 		
 		tableData.setOpaque(true);
 		tableData.setEnabled(false);
 		dtm.addTableModelListener(new TableModelListener()
 		{
 			public void tableChanged(TableModelEvent tme)
 			{
	 			//System.out.println("table changed");
	 			//tableData.setModel(dtm);
	 			//System.out.println(dtm.getDataVector());
	 			//dtm.newDataAvailable(tme);
 			}
 		});
 		
 		
 		scrollPane=new JScrollPane(tableData);
 		//scrollPane.setBackground(Color.getHSBColor(f[0],f[1],f[2]));
 		scrollPane.setBounds(20,20,510,230);
 		jtbWorkspace=new JTabbedPane();
		jtbWorkspace.setTabPlacement(JTabbedPane.TOP);
		workspace=new Workspace();
		jtbWorkspace.addTab("Working_Area",workspace);
		summary=new Summary();
		jtbWorkspace.addTab("Report_Summary",summary );
		jtbWorkspace.setBounds(5,20,540,300);

		jtbWorkspace.setVisible(true);
		pan3.add(jtbWorkspace,BorderLayout.CENTER);
		
 		workspace.add(scrollPane);
 		
 		report=new StringBuffer("<html><body>Filename: "+fname+"<br>");
		report.append("Number of records: "+dtm.getRowCount()+"<br>");
		report_summary=new JLabel(report.toString());
		scroll=new JScrollPane(report_summary);
	//	scroll.setBackground(Color.getHSBColor(f[0],f[1],f[2]));
		
		scroll.setBounds(20,10,500,250);
		summary.add(scroll);
		report_summary.setBounds(20,20,500,200);
		//report_summary.setBackground(Color.getHSBColor(f[0],f[1],f[2]));
		//summary.add(report_summary);
	
 		pan3.repaint();
 		//mainPanel.add(pan3);
		resultSet.reset();
		dataSet.reset();
	
	}
	
	void RemoveMissingRows(ArrayList lst)
	{
		int cnt=0;
		//System.out.print("lstsize:"+lst.size());
		Vector v=dtm.getDataVector();
		report.append("<br>You chose Removing of missing values<br>Rows removed are: <br>");
		if(lst.size()>0)
		{
			int msg=JOptionPane.showConfirmDialog(mainPanel,"Are you sure u want to delete all missing records?");
					
			if(msg==0)
			{
				for(int i=0;i<lst.size();i++)
				{
					//String str=;
					Object o=v.get(Integer.parseInt(lst.get(i).toString())-cnt);
					report.append((i+1)+" : "+o+"<br>");
					dtm.removeRow(Integer.parseInt(lst.get(i).toString())-cnt);
					cnt++;
					cntRow--;
				}
			
				JOptionPane.showMessageDialog(null,""+lst.size()+" missing values found and removed!!");
			}
		}
		else
			JOptionPane.showMessageDialog(null,"No missing values found !!");
		report_summary.setText(report.toString());
		summary.repaint();
	}
	
	

	class Workspace extends JPanel
	{
		Workspace()
		{
			//System.out.print("in workspace");
			this.setLayout(null);
			this.setBounds(0,0,530,310);
		//	this.setBackground(Color.getHSBColor(f[0],f[1],f[2]));
		}
		
	}
	
	class Summary extends JPanel
	{
		Summary()
		{
			//System.out.print("in summary");
			this.setLayout(null);
			this.setBounds(0,0,530,310);
			report_summary=new JLabel("");
			this.add(report_summary);
				
		//	this.setBackground(Color.getHSBColor(f[0],f[1],f[2]));
		}
	}
	
}
